#include "qrulegroupmodel.h"

QRuleGroupModel::QRuleGroupModel(vector<RuleGroup> *rulegroups, QObject* parent) : QAbstractTableModel(parent)
{
    rule_groups = rulegroups;

}

int QRuleGroupModel::rowCount(const QModelIndex &index) const
{
    return rule_groups->size();

}

int QRuleGroupModel::columnCount(const QModelIndex &index) const
{
    return 8;
}


// returns the item of data that corresponds to the index argument
QVariant QRuleGroupModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();

    if (index.row() < 0 || index.row() >= rule_groups->size())
        return QVariant();

    RuleGroup rule_group = rule_groups->at(index.row());

    if(role==Qt::CheckStateRole) {
        if(index.column()==0)
            Qt::Unchecked;
    }

    if (role == Qt::DisplayRole) {
        switch(index.column()) {
             case 1:
            return "last mod";

             case 2:
            return rule_group.author.c_str();

             case 3:
            return rule_group.name.c_str();

             case 4:
            return rule_group.description.c_str();

             case 5:
            return "1";

             case 6:
            return "0";

             case 7:
            return "0";

             default:
            return QVariant();
        }
    }
    return QVariant();
}

QVariant QRuleGroupModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if(role!=Qt::DisplayRole)
        return QVariant();

    if (role != Qt::DisplayRole)
        return QVariant();

    if (orientation == Qt::Horizontal) {
        switch (section) {
             case 0:
            return tr(" ");

             case 1:
            return tr("Last Mod");

             case 2:
            return tr("Author");

             case 3:
            return tr("RuleGroup Name");

             case 4:
            return tr("Description");

             case 5:
            return tr("Rules");

             case 6:
            return tr("Rating");

             case 7:
            return tr("Downloads");

             default:
            return QVariant();
        }
    }
    return QVariant();
}


bool QRuleGroupModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (index.isValid() && role == Qt::EditRole)
    {
        int row = index.row();

        Rule rule;

        /*if(value.canConvert<Rule>()) {
            rule = value.value<Rule>();
            rules->push_back(rule);
            emit dataChanged(index, index);
            return true;
        }*/
    }

    return false;
}



// need to implement this for editable models
Qt::ItemFlags QRuleGroupModel::flags(const QModelIndex &index) const
{
    if (!index.isValid())
        return Qt::ItemIsEnabled;

    return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
}

bool QRuleGroupModel::insertRows(int position, int rows, const QModelIndex &index)
{
    return true;

}
bool QRuleGroupModel::removeRows(int position, int rows, const QModelIndex &index)
{
    return true;
}

